PROJECT = /Users/ricardo/text-categorization-som/
EX_IDM    = ../../../test/ex_idm.sh
#PROFILER  = python -m cProfile -s time
#PROFILER  = python -m memory_profiler
PROFILER  = time python
LEARN = ./learn.py
CLASSIFY = ./classify.py
PLOT = ./plot.py
PLOTMAP = ./plotmap.py

# Correctness test params
C_DIM        = 10
C_TS_SIZE    = 10
C_TDATA      = $(PROJECT)/data/sparse_idm/id10_1000.dat
C_SIZE_X     = 100
C_SIZE_Y     = 100
C_SOM_INI    = c_som_ini.dat
C_SOM_FIN    = c_som_fin.dat
C_CLASS_DATA = c_class.dat
# avoid showing progress per iter chunk, epochs are frequent enough
C_SHOW_PROG  = 100000
C_LEARN_OUT  = c_learn.out
C_CLASS_OUT  = c_class.out
C_MIN_W      = 0
C_MAX_W      = 0.1
C_EPOCHS     = 2
C_DEBUG      = 0

# Grimm tales (semantic maps)
G_PRECLASS    = $(PROJECT)/data/semap2/vocab.txt
G_COLORMAP    = g_color.map
G_TDATA_LEARN = $(PROJECT)/data/semap2/encdata_learn.txt
G_TDATA_CLASS = $(PROJECT)/data/semap2/encdata_class.txt
G_VOCAB_SIZE  = $(shell cat  $(G_PRECLASS) | wc -l)
# dimension of input space is 3 times vocab size (as our inputs are trigrams)
G_DIM         = $(shell echo "3*$(G_VOCAB_SIZE)" | bc) 
G_TS_SIZE     = $(shell cat $(G_TDATA_LEARN) | wc -l)
# we need a mesh that can hold %75 of vocabulary 
G_SIZE_X      = $(shell echo "scale=0; (75*$(G_VOCAB_SIZE))/100" | bc) 
G_SIZE_Y      = $(G_SIZE_X)
G_SOM_INI     = g_som_ini.dat
G_SOM_FIN     = g_som_fin.dat
G_CLASS_DATA  = g_class.dat
G_SHOW_PROG   = 100
G_LEARN_OUT   = g_learn.out
G_CLASS_OUT   = g_class.out
G_MIN_W       = 0
G_MAX_W       = 0.1
G_EPOCHS      = 1
G_DEBUG       = 0
G_PLOT_OUT    = g_plot.png
G_PLOTMAP_OUT = g_plotmap.png

# Encyclopaedia Britannica (semantic maps)
B_PRECLASS    = $(PROJECT)/data/semap4/vocab.txt
B_COLORMAP    = g_color.map
B_TDATA_LEARN = $(PROJECT)/data/semap4/encdata_learn.txt
B_TDATA_CLASS = $(PROJECT)/data/semap4/encdata_class.txt
B_VOCAB_SIZE  = $(shell cat  $(B_PRECLASS) | wc -l)
# dimension of input space is 3 times vocab size (as our inputs are trigrams)
B_DIM         = $(shell echo "3*$(B_VOCAB_SIZE)" | bc) 
B_TS_SIZE     = $(shell cat $(B_TDATA_LEARN) | wc -l)
# we need a mesh that can hold %75 of vocabulary 
B_SIZE_X      = $(shell echo "scale=0; (75*$(B_VOCAB_SIZE))/100" | bc) 
B_SIZE_Y      = $(B_SIZE_X)
B_SOM_INI     = b_som_ini.dat
B_SOM_FIN     = b_som_fin.dat
B_SHOW_PROG   = 100
B_LEARN_OUT   = b_learn.out
B_MIN_W       = 0
B_MAX_W       = 0.1
B_EPOCHS      = 1
B_DEBUG       = 0
B_PLOTMAP_OUT = b_plotmap.png

all: learn_g plotmap_g

learn_c: $(LEARN)
	$(PROFILER) $(LEARN) $(C_DIM) $(C_TS_SIZE) $(C_TDATA) $(C_SIZE_X) $(C_SIZE_Y) $(C_MIN_W) $(C_MAX_W) $(C_SOM_INI) $(C_SOM_FIN) $(C_EPOCHS) $(C_DEBUG) $(C_SHOW_PROG) > $(C_LEARN_OUT) 2>&1
		$(EX_IDM) $(C_SOM_FIN) >> $(C_LEARN_OUT)
	tail -$(C_DIM) $(C_LEARN_OUT)

class_c: $(CLASSIFY)
	$(PROFILER) $(CLASSIFY) $(C_DIM) $(C_TS_SIZE) $(C_TDATA) $(C_SIZE_X) $(C_SIZE_Y) $(C_SOM_FIN) $(C_CLASS_DATA) $(C_DEBUG) $(C_SHOW_PROG) > $(C_CLASS_OUT) 2>&1

learn_g: $(LEARN)
	@printf "%s,   %s,   %s,   %s\n" \
         "vocab size = $(G_VOCAB_SIZE)" \
         "input dim = $(G_DIM)" \
         "trainset size = $(G_TS_SIZE)"  \
         "som dim = [$(G_SIZE_X),$(G_SIZE_Y)]" > $(G_LEARN_OUT)
	@tail -1 $(G_LEARN_OUT)
	$(PROFILER) $(LEARN) $(G_DIM) $(G_TS_SIZE) $(G_TDATA_LEARN) $(G_SIZE_X) $(G_SIZE_Y) $(G_MIN_W) $(G_MAX_W) $(G_SOM_INI) $(G_SOM_FIN) $(G_EPOCHS) $(G_DEBUG) $(G_SHOW_PROG) >> $(G_LEARN_OUT) 2>&1

class_g: $(CLASSIFY)
	$(PROFILER) $(CLASSIFY) $(G_DIM) $(G_VOCAB_SIZE) $(G_TDATA_CLASS) $(G_SIZE_X) $(G_SIZE_Y) $(G_SOM_FIN) $(G_CLASS_DATA) $(G_DEBUG) $(G_SHOW_PROG) > $(G_CLASS_OUT) 2>&1

plot_g: $(PLOT)
	$(PROFILER) $(PLOT) $(G_CLASS_DATA) $(G_PRECLASS) $(G_COLORMAP) $(G_PLOT_OUT)
	display $(G_PLOT_OUT)

plotmap_g: $(PLOTMAP)
	$(PROFILER) $(PLOTMAP) $(G_DIM) $(G_VOCAB_SIZE) $(G_TDATA_CLASS) $(G_SIZE_X) $(G_SIZE_Y) $(G_SOM_FIN) $(G_PRECLASS) $(G_COLORMAP) $(G_PLOTMAP_OUT)
	display $(G_PLOTMAP_OUT)

learn_b: $(LEARN)
	@printf "%s,   %s,   %s,   %s\n" \
         "vocab size = $(B_VOCAB_SIZE)" \
         "input dim = $(B_DIM)" \
         "trainset size = $(B_TS_SIZE)"  \
         "som dim = [$(B_SIZE_X),$(B_SIZE_Y)]" > $(B_LEARN_OUT)
	@tail -1 $(B_LEARN_OUT)
	$(PROFILER) $(LEARN) $(B_DIM) $(B_TS_SIZE) $(B_TDATA_LEARN) $(B_SIZE_X) $(B_SIZE_Y) $(B_MIN_W) $(B_MAX_W) $(B_SOM_INI) $(B_SOM_FIN) $(B_EPOCHS) $(B_DEBUG) $(B_SHOW_PROG) >> $(B_LEARN_OUT) 2>&1

plotmap_b: $(PLOTMAP)
	$(PROFILER) $(PLOTMAP) $(B_DIM) $(B_VOCAB_SIZE) $(B_TDATA_CLASS) $(B_SIZE_X) $(B_SIZE_Y) $(B_SOM_FIN) $(B_PRECLASS) $(B_COLORMAP) $(B_PLOTMAP_OUT)
	display $(B_PLOTMAP_OUT)

clean:
	rm -fr nohup.out *.pyc ./build *.so *.c __pycache__
	rm -f $(C_SOM_INI) $(C_SOM_FIN) $(C_LEARN_OUT) $(C_CLASS_OUT) $(C_CLASS_DATA)
	rm -f $(G_SOM_INI) $(G_SOM_FIN) $(G_LEARN_OUT) $(G_CLASS_OUT) $(G_CLASS_DATA) $(G_PLOT_OUT) $(G_PLOTMAP_OUT)
	rm -f $(B_SOM_INI) $(B_SOM_FIN) $(B_LEARN_OUT) $(B_CLASS_OUT) $(B_CLASS_DATA) $(B_PLOT_OUT) $(B_PLOTMAP_OUT)
